package com.hitqz.robot.biz.controller;

import com.hitqz.robot.api.business.dto.VisionDetectSaveDTO;
import com.hitqz.robot.api.business.dto.openapi.CvdGuideTaskDTO;
import com.hitqz.robot.api.business.dto.openapi.CvdQuestionTextDTO;
import com.hitqz.robot.api.business.entity.CvdGuidePosEntity;
import com.hitqz.robot.biz.service.ApiService;
import com.hitqz.robot.biz.service.VisionDetectService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kehanjiang
 * @date 2025/09/23
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/open-api")
@Tag(description = "open-api", name = "开放接口管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ApiController {
    private final VisionDetectService visionDetectService;
    private final ApiService apiService;

    /************************* 视觉定时检测 *************************/
    /**
     * 新增视觉检测记录
     *
     * @param dto 视觉检测记录
     * @return R
     */
    @Operation(summary = "新增视觉检测记录", description = "新增视觉检测记录")
    @PostMapping("/vision/notify")
    public R<Boolean> visionNotify(@RequestBody VisionDetectSaveDTO dto) {
        try {
            return R.ok(visionDetectService.saveVisionDetect(dto));
        } catch (Exception e) {
            log.error("新增视觉检测记录失败:{}", e.getMessage(), e);
            return R.failed("新增视觉检测记录失败:" + e.getMessage());
        }
    }
    /************************* 视觉定时检测 *************************/


    /************************* 迎宾讲解 *************************/
    @Operation(summary = "配置迎宾讲解任务", description = "配置迎宾讲解任务")
    @PostMapping("/guide/modify")
    public R<Boolean> guideModify(@RequestBody CvdGuideTaskDTO dto) {
        try {
            return R.ok(apiService.guideModify(dto));
        } catch (Exception e) {
            log.error("配置迎宾讲解任务失败:{}", e.getMessage(), e);
            return R.failed("配置迎宾讲解任务失败:" + e.getMessage());
        }
    }

    @Operation(summary = "获取迎宾讲解任务详情", description = "获取迎宾讲解任务详情")
    @GetMapping("/guide/detail")
    public R<CvdGuideTaskDTO> guideDetail() {
        try {
            return R.ok(apiService.guideDetail());
        } catch (Exception e) {
            log.error("获取迎宾讲解任务详情失败:{}", e.getMessage(), e);
            return R.failed("获取迎宾讲解任务详情失败:" + e.getMessage());
        }
    }

    @Operation(summary = "获取迎宾讲解位置列表", description = "获取迎宾讲解位置列表")
    @GetMapping("/guide/posList")
    public R<List<CvdGuidePosEntity>> guidePosList() {
        try {
            return R.ok(apiService.guidePosList());
        } catch (Exception e) {
            log.error("获取迎宾讲解位置列表失败:{}", e.getMessage(), e);
            return R.failed("获取迎宾讲解位置列表失败:" + e.getMessage());
        }
    }

    @Operation(summary = "迎宾讲解操作", description = "迎宾讲解操作")
    @GetMapping("/guide/operate/{cmd}")
    public R<Boolean> guideOperate(@PathVariable("cmd")
                                   @Parameter(description = "操作：0-开始任务 1-暂停任务 2-继续任务 3-终止任务 4-跳过讲解 5-重复讲解") String cmd) {
        try {
            return R.ok(apiService.guideOperate(cmd));
        } catch (Exception e) {
            log.error("迎宾讲解操作失败:{}", e.getMessage(), e);
            return R.failed("迎宾讲解操作失败:" + e.getMessage());
        }
    }
    /************************* 迎宾讲解 *************************/


    /************************* 语音交互 *************************/
    @Operation(summary = "语音交互操作-音频输入", description = "语音交互操作-音频输入")
    @GetMapping("/question/operate/{cmd}")
    public R<Boolean> questionOperate(@PathVariable("cmd") @Parameter(description = "操作：0-唤醒 1-关闭 ") String cmd) {
        try {
            return R.ok(apiService.questionVoiceOperate(cmd));
        } catch (Exception e) {
            log.error("语音交互操作-音频输入 失败:{}", e.getMessage(), e);
            return R.failed("语音交互操作-音频输入 失败:" + e.getMessage());
        }
    }

    @Operation(summary = "语音交互操作-文字输入", description = "语音交互操作-文字输入")
    @PostMapping("/question/operate/text")
    public R<Boolean> questionTextOperate(@RequestBody CvdQuestionTextDTO dto) {
        try {
            return R.ok(apiService.questionTextOperate(dto));
        } catch (Exception e) {
            log.error("语音交互操作-文字输入 失败:{}", e.getMessage(), e);
            return R.failed("语语音交互操作-文字输入 失败:" + e.getMessage());
        }
    }

    /************************* 语音交互 *************************/

}